import java.util.Arrays;

public class Leetcode283_移动零 {
    public static void main(String[] args) {
        Solution1 solution1 = new Solution1();
        int[] nums = new int[]{0, 1, 0, 3, 12};
        solution1.moveZeroes(nums);
    }
}


class Solution1 {
    public void moveZeroes(int[] nums) {
        // i为指向非零元素的指针，j为指向元素0的指针
        int i = 0, j = 0;

        // 遍历nums数组，非0元素往前移动，同时j（元素0）的数量+1
        for (int k = 0; k < nums.length; k++) {
            if (nums[k] != 0) {
                nums[i++] = nums[k];
            } else {
                j++;
            }
        }

        // 往nums数组末端添加j个0元素
        for (int k = 0; k < j; k++) {
            nums[nums.length - 1 - k] = 0;
        }
    }
}